<?php

/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 10/23/14
 * Time: 6:27 PM
 */
class DBTypes
{

  const DB_DATE = 'date';
  const DB_TEXT = 'text';
  const DB_INT = 'int';
  const DB_BOOL = 'tinyint(1)';
  const DB_VARCHAR = 'varchar(40)';

  const INPUT_DATE = 'date';
  const TEXTAREA_TEXT = 'textarea';
  const INPUT_VARCHAR = 'text';
  const INPUT_INT = 'text';
  const INPUT_BOOL = 'checkbox';

  /**
   * @param $dbType
   * @param $name
   * @param $id [optional]
   * @return string
   */
  static function generateInput($dbType, $name, $id)
  {

    $output = "";
    $id = $id ? "id = '" . $id . "'" : "";

    switch ($dbType) {

      case self::DB_BOOL || self::DB_DATE || self::DB_INT || self::DB_VARCHAR:
        $output .= "<input type = '" . self::getInputType($dbType) . "' name = '" . $name . "' " . $id . ">";
        break;
      case self::DB_TEXT: {
        $output .= "<textarea id = '" . $id . "' name = '" . $name . "'></textarea>";
      }

    }

    return $output;

  }

  static function getInputType($dbType)
  {

    switch ($dbType) {
      case self::DB_INT:
        return self::INPUT_INT;
      case self::DB_VARCHAR:
        return self::INPUT_VARCHAR;
      case self::DB_DATE:
        return self::INPUT_DATE;
      case self::DB_BOOL:
        return self::INPUT_BOOL;
      case self::DB_TEXT:
        return self::TEXTAREA_TEXT;
      default:
        return self::INPUT_VARCHAR;
    }
  }

  static function generateOptionsOfTypes()
  {

    $output = "";

    $output .= "<option value='" . self::DB_TEXT . "'> Длинное текстовое поле</option>";
    $output .= "<option value='" . self::DB_VARCHAR . "'> Короткое текстовое поле</option>";
    $output .= "<option value='" . self::DB_INT . "'> Числовое поле</option>";
    $output .= "<option value='" . self::DB_DATE . "'> Дата</option>";
    $output .= "<option value='" . self::DB_BOOL . "'> Флажок</option>";

    echo $output;
  }
} 